package cn.edu.ctbu.api01.Controller;
import cn.edu.ctbu.api01.domain.Student;
import cn.edu.ctbu.api01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/list")
    public String list(Model model, @RequestParam(required = false) String search) {
        List<Student> students;
        if (search != null && !search.isEmpty()) {
//            System.out.println("Received name parameter: " + search); // 添加日志
            students = studentService.findByName(search);
        } else {
            students = studentService.findAll();
        }
        model.addAttribute("students", students);
//        model.addAttribute("searchName", search); // 将搜索的姓名也传递到前端页面
        return "student/list";
    }
}